package com.axc.gallery2.edit.overLay;

import android.graphics.Bitmap;
import android.util.Log;

public class DecorateImage {
	IOverLaybaseMethod method = null;
	// 扩展拉着触摸点范围
	int extenswidth = 0;

	public DecorateImage(IOverLaybaseMethod m) {
		method = m;
	}

	/**
	 * 装饰图片旋转
	 * 
	 * @param mx
	 *            中间坐标x
	 * @param my
	 *            中间坐标y
	 * @param miDTX
	 *            移动后x
	 * @param miDTY
	 *            移动后y
	 * @param nguiderWidth
	 *            新的宽度
	 * @param nguiderHeight
	 *            新的高度
	 * @param guiderBitmap
	 *            引导图标
	 * @param r
	 *            半径
	 * @param tx
	 *            触摸坐标
	 * @param ty
	 *            触摸坐标
	 */
	public void ImgSpin(int mx, int my, int miDTX, int miDTY, int nguiderWidth,
			int nguiderHeight, Bitmap guiderBitmap, int r, int tx, int ty,
			boolean cansipn, boolean haschange) {
		mx = miDTX - guiderBitmap.getWidth() / 2 + r;
		my = miDTY - guiderBitmap.getHeight() / 2 + r;
		nguiderWidth = guiderBitmap.getWidth() - (2 * r);
		nguiderHeight = guiderBitmap.getHeight() - (2 * r);

		// 触摸判断是的范围
		int rangew = guiderBitmap.getWidth() / 6;
		int rangeh = guiderBitmap.getHeight() / 6;

		// top
		if (tx >= mx + nguiderWidth / 2 - rangew
				&& tx <= mx + nguiderWidth / 2 + rangew && ty >= my - rangeh
				&& ty <= my + rangeh) {
			cansipn = haschange = true;
			CanSpin(cansipn, haschange);
		}
		// bottom
		else if (tx >= mx + nguiderWidth / 2 - rangew
				&& tx <= mx + nguiderWidth / 2 + rangew
				&& ty >= my + nguiderHeight - rangeh
				&& ty <= my + nguiderHeight + rangeh) {
			cansipn = haschange = true;
			CanSpin(cansipn, haschange);
		}
		// left
		else if (tx >= mx - rangew && tx <= mx + rangew
				&& ty >= my + nguiderHeight / 2 - rangew
				&& ty <= my + nguiderHeight / 2 + rangew) {
			cansipn = haschange = true;
			CanSpin(cansipn, haschange);
			// right
		} else if (tx >= mx + nguiderWidth - rangew
				&& tx <= mx + nguiderWidth + rangew
				&& ty >= my + nguiderHeight / 2 - rangeh
				&& ty <= my + nguiderHeight / 2 + rangeh) {
			cansipn = haschange = true;
			CanSpin(cansipn, haschange);
		} else {
			cansipn = haschange = false;
			CantSpin(cansipn, haschange);
		}
	}

	/**
	 * 装饰图缩放
	 * 
	 * @param mx
	 *            中间坐标x
	 * @param my
	 *            中间坐标y
	 * @param miDTX
	 *            移动后x
	 * @param miDTY
	 *            移动后y
	 * @param nguiderWidth
	 *            新的宽度
	 * @param nguiderHeight
	 *            新的高度
	 * @param guiderBitmap
	 *            引导图标
	 * @param canZoom
	 * @param haschange
	 * @param r
	 *            半径
	 * @param tx
	 *            触摸坐标
	 * @param ty
	 *            触摸坐标
	 */
	public void picZooms(int mx, int my, int miDTX, int miDTY,
			int nguiderWidth, int nguiderHeight, boolean canZoom,
			boolean haschange, Bitmap guiderBitmap, int r, int tx, int ty) {
		mx = miDTX - guiderBitmap.getWidth() / 2 + r;
		my = miDTY - guiderBitmap.getHeight() / 2 + r;
		nguiderWidth = guiderBitmap.getWidth() - (2 * r);
		nguiderHeight = guiderBitmap.getHeight() - (2 * r);

		// 其实焦点miDtx+r//左上角顶点
		int newX1 = mx;// mx-(nguiderWidth/2)+(top.getWidth()/2);
		int newY1 = my;// my-(nguiderHeight/2)+(top.getHeight()/2);
		// 左下角顶点
		int newX2 = newX1;
		int newY2 = newY1 + nguiderHeight;
		// 右上角顶点
		int newX3 = newX1 + nguiderWidth;
		int newY3 = newY1;
		// 右下角顶点
		int newX4 = newX3;
		int newY4 = newY2;

		int rangew = guiderBitmap.getWidth() / 6;
		int rangeh = guiderBitmap.getHeight() / 6;

		// 在左上角
		if (tx >= mx && tx <= mx + rangew && ty >= my && ty <= my + rangeh) {
			CanZoom(canZoom, haschange);
			method.Mark(1);
		}

		// 左下角
		else if (tx >= newX2 - rangew && tx <= newX2 + rangew
				&& ty >= newY2 - rangeh && ty <= newY2 + rangeh) {
			CanZoom(canZoom, haschange);
			method.Mark(4);
		}
		// 右上角
		else if (tx >= newX3 - rangew && tx <= newX3 + rangew
				&& ty >= newY3 - rangeh && ty <= newY3 + rangeh) {
			CanZoom(canZoom, haschange);
			method.Mark(2);
		}
		// 右下角
		else if (tx >= newX4 - rangew && tx <= newX4 + rangew
				&& ty >= newY4 - rangeh && ty <= newY4 + rangeh) {
			CanZoom(canZoom, haschange);
			method.Mark(3);

		} else {
			CantZoom(canZoom, haschange);
			method.Mark(0);
		}
	}

	public void CanZoom(boolean canZoom, boolean haschange) {
		canZoom = haschange = true;
		method.CanZoom(canZoom);
		method.HasChange(haschange);
		method.CanSpin(false);
	}

	public void CantZoom(boolean canZoom, boolean haschange) {
		canZoom = haschange = false;
		method.CanZoom(canZoom);
		method.HasChange(haschange);
	}

	public void CanSpin(boolean canSpin, boolean haschange) {
		canSpin = haschange = true;
		method.CanSpin(canSpin);
		method.HasChange(haschange);
		method.CanZoom(false);
		Log.w("can", String.valueOf(canSpin));
	}

	public void CantSpin(boolean canSpin, boolean haschange) {
		canSpin = haschange = false;
		method.CanSpin(canSpin);
		method.HasChange(haschange);
		Log.w("can", String.valueOf(canSpin));
	}

}
